import string
from google.appengine.ext import db
import logging
from google.appengine.api import memcache

class AlreadyRegistered(Exception):
    pass

class InvalidKey(Exception):
    pass

def get_counter_cnt(counter_type,cache_key,on_get_zero):
    cnt=memcache.get(cache_key)
    if cnt is None:
        counter=counter_type()
        cnt=counter.get()
        memcache.add(cache_key,cnt,counter.cache_time_out)
    if cnt == 0:
        cnt=on_get_zero()
    
    return cnt

    

def get_obj(obj_type,key_name,on_get_None=None):
    obj=memcache.get(key_name)
    if obj is None:
        obj=obj_type.get_by_key_name(key_name)
        if obj is not None:
            memcache.add(key_name,obj,obj.cache_time_out)
    if obj is None:
        obj=on_get_None()
    return obj

def save_obj(obj,key_name):
    memcache.delete(key_name)
    obj.save()
    